package com.bff.gaia.mix.api.xjoin.core.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {

	private static Logger logger = LoggerFactory.getLogger(MD5Utils.class);

	public static String getMD5String(String value) {
		try {
			MessageDigest md = MessageDigest.getInstance("md5");
			byte[] e = md.digest(value.getBytes());
			return toHexString(e);
		} catch (NoSuchAlgorithmException e) {
			logger.error("MD5Utils || getMD5String" +e.toString());
			return value;
		}
	}

	private static String toHexString(byte bytes[]) {
		StringBuilder hs = new StringBuilder();
		String stmp = "";
		for (int n = 0; n < bytes.length; n++) {
			stmp = Integer.toHexString(bytes[n] & 0xff);
			if (stmp.length() == 1){
				hs.append("0").append(stmp);
			}else{
				hs.append(stmp);
			}
		}
		return hs.toString();
	}
}